using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Emlyn.Common.Tools.BusinessLayer;

namespace Emlyn.Website.Articles
{
    public class ArticleQuery: BaseQuery<Article, Guid, ArticleDataLayer>
    {
        public ArticleQuery()
            : base(new ArticleDataLayer())
        {
        }

        public Article GetArticleByArticleCode(string aArticleCode)
        {
            return ConstructInstanceFromDataSet(_dl.GetArticleByArticleCode(aArticleCode));
        }

        public IList<Article> GetArticlesForParentArticleID(Guid aParentArticleID)
        {
            return ConstructInstancesFromDataSet(_dl.GetArticlesByParentArticleID(aParentArticleID));
        }

        public IList<Article> GetArticlesNotForParentArticleID(Guid aParentArticleID)
        {
            return ConstructInstancesFromDataSet(_dl.GetArticlesNotForParentArticleID(aParentArticleID));
        }

        public IList<Article> GetArticlesForChildArticleID(Guid aChildArticleID)
        {
            return ConstructInstancesFromDataSet(_dl.GetArticlesByChildArticleID(aChildArticleID));
        }

        public IList<Article> GetArticlesNotForChildArticleID(Guid aChildArticleID)
        {
            return ConstructInstancesFromDataSet(_dl.GetArticlesNotForChildArticleID(aChildArticleID));
        }

        public IList<Article> GetArticlesWithNoParent()
        {
            return ConstructInstancesFromDataSet(_dl.GetArticlesWithNoParent());
        }

        protected override Article ConstructInstanceFromRow(DataRow lrow)
        {
            Article larticle = new Article((Guid)lrow["ArticleID"]);
            larticle.ArticleCode = Convert.ToString(lrow["ArticleCode"]).Trim();
            larticle.ArticleName = Convert.ToString(lrow["ArticleName"]).Trim();
            larticle.Url = Convert.ToString(lrow["Url"]).Trim();
            larticle.ArticleContent = Convert.ToString(lrow["ArticleContent"]).Trim();
            larticle.LastChanged = Convert.ToDateTime(lrow["LastChanged"]);
            return larticle;
        }
    }
}
